package com.sh.data.engine.common.util;

import cn.hutool.core.util.StrUtil;
import com.sh.data.engine.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;

import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.ServiceUnavailableException;
import java.io.IOException;
import java.rmi.ConnectException;

/**
 * @author tyf
 * @description:
 * @date 2020-11-12 18:19
 */
@Slf4j
public class JmxUtil {

    public static void closeClient(String jmxServer, int jmxPort) {
        String ip = StrUtil.isBlank(jmxServer) ? "localhost" : jmxServer;
        JMXConnector connector = null;
        try {
            String jmxServerName = "client";
            String url = "service:jmx:rmi:///jndi/rmi://" + ip + ":" + jmxPort + "/" + jmxServerName;
            JMXServiceURL address = new JMXServiceURL(url);
            connector = JMXConnectorFactory.connect(address);
            MBeanServerConnection mbsc = connector.getMBeanServerConnection();
            connector.connect();

            ObjectName objectName = new ObjectName(jmxServerName + ":name=point");

            mbsc.invoke(objectName, "close", null, null);
            // PointMBean hello = JMX.newMBeanProxy(mbsc, objectName, PointMBean.class);
            // hello.close();
        } catch (IOException e) {
            if (e.getCause() instanceof ServiceUnavailableException) {
                log.error(e.getMessage(), e);
            } else {
                log.error(e.getMessage(), e);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            if (connector != null) {
                try {
                    connector.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }

    public static Point checkPoint(Integer jmxPort) {

        String ip = "localhost";

        Point point = new Point();
        JMXConnector connector = null;
        try {
            String jmxServerName = "client";
            String url = "service:jmx:rmi:///jndi/rmi://" + ip + ":" + jmxPort + "/" + jmxServerName;
            JMXServiceURL address = new JMXServiceURL(url);
            connector = JMXConnectorFactory.connect(address);
            MBeanServerConnection mbsc = connector.getMBeanServerConnection();
            connector.connect();

            ObjectName objectName = new ObjectName(jmxServerName + ":name=point");
            PointMBean hello = JMX.newMBeanProxy(mbsc, objectName, PointMBean.class);
            point.setLastSyncTime(hello.getLastSyncTime());
            point.setDelay(hello.getDelay());
            point.setFailNum(hello.failNum());
            point.setType(hello.getType());
        } catch (IOException e) {
            if (e.getCause() instanceof ServiceUnavailableException) {
                throw new BusinessException("客户端进程不存在");
            } else {
                log.error(e.getMessage(), e);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            if (connector != null) {
                try {
                    connector.close();
                } catch (IOException e) {
                    if (e instanceof ConnectException) {
                        throw new BusinessException("客户端进程不存在");
                    } else {
                        log.error(e.getMessage(), e);
                    }
                }
            }
        }

        return point;
    }
}
